#include "HNPrivateScenceBase.h"
#include "Game/GameLib.h"
#include "kernel/kernel/server/CServerItem.h"

using namespace cocos2d;
using namespace ui;

HNPrivateScenceBase::HNPrivateScenceBase()
	:m_iJoinCout(0), m_exchangeScore(0.00)
	, m_channelIndex(0)
	,m_eLinkAction(Type_Link_NULL)
{
	zeromemory(&m_kCreatePrivateNet,sizeof(m_kCreatePrivateNet));
	zeromemory(&m_kPrivateInfo,sizeof(m_kPrivateInfo));
	memset(m_kJoinNumTxt,0,sizeof(m_kJoinNumTxt));
	CServerItem::get()->SetServerPrivateSink(this);
}
HNPrivateScenceBase::~HNPrivateScenceBase()
{

}

void HNPrivateScenceBase::OnSocketSubPrivateInfo(CMD_GR_Private_Info* pNetInfo)
{
	m_kPrivateInfo = *pNetInfo;

	if (m_eLinkAction == Type_Link_Create)
	{
		CServerItem::get()->SendSocketData(MDM_GR_PRIVATE,SUB_GR_CREATE_PRIVATE,&m_kCreatePrivateNet,sizeof(m_kCreatePrivateNet));
		zeromemory(&m_kCreatePrivateNet,sizeof(m_kCreatePrivateNet));
	}
	if (m_eLinkAction == Type_Link_Join)
	{
		CMD_GR_Join_Private kSendNet;
		kSendNet.dwRoomNum = utility::parseInt(m_kJoinNumTxt);
		kSendNet.bScoreCount = m_exchangeScore;
		CServerItem::get()->SendSocketData(MDM_GR_PRIVATE,SUB_GR_JOIN_PRIVATE,&kSendNet,sizeof(kSendNet));
		m_exchangeScore = 0.00;
	}
	m_eLinkAction = Type_Link_NULL;
}

void HNPrivateScenceBase::upJoinNumTxt()
{
	for (int i = 0;i<m_iJoinCout;i++)
	{
		Sprite* imgValue = (Sprite*)joinRoomPlane->getChildByName(StringUtils::format("Sprite_value%d", i + 1));
		imgValue->setTexture(StringUtils::format("room/UI_fkpt_jrgj_font%d.png", (m_kJoinNumTxt[i] - '0')));
		imgValue->setVisible(true);
	}
	for (int i = m_iJoinCout;i<6;i++)
	{
		Sprite* imgValue = (Sprite*)joinRoomPlane->getChildByName(StringUtils::format("Sprite_value%d", i + 1));
		imgValue->setVisible(false);
	}
}

void HNPrivateScenceBase::OnSocketSubPrivateCreateSuceess(CMD_GR_Create_Private_Sucess* pNetInfo)
{
	//NoticeMsg::Instance().ShowTopMsg(utility::toString((int)pNetInfo->dwRoomNum));
}
void HNPrivateScenceBase::OnSocketSubPrivateRoomInfo(CMD_GF_Private_Room_Info* pNetInfo)
{
	m_kPrivateRoomInfo = *pNetInfo;
}
void HNPrivateScenceBase::OnSocketSubPrivateEnd(CMD_GF_Private_End_Info* pNetInfo)
{

}
void HNPrivateScenceBase::OnSocketSubPrivateDismissInfo(CMD_GF_Private_Dismiss_Info* pNetInfo)
{

}

void HNPrivateScenceBase::OnSocketSubPrivateDiamondInfo(CMD_GR_REFRESH_DIAMOND* pNetInfo)
{

}

void HNPrivateScenceBase::Button_SureeDismissPrivate()
{
	CMD_GR_Dismiss_Private kNetInfo;
	kNetInfo.bDismiss = 1;
	CServerItem::get()->SendSocketData(MDM_GR_PRIVATE,SUB_GR_PRIVATE_DISMISS,&kNetInfo,sizeof(kNetInfo));
}
void HNPrivateScenceBase::Button_DismissPrivate()
{
	/*
	if (m_kPrivateRoomInfo.dwPlayCout == 0)
	{
		PopScence::Instance().showAccessPlane(utility::getScriptString("PrivateTxt0"),this,
			button_selector(HNPrivateScenceBase::Button_SureeDismissPrivate),NULL);
		return;
	}
	*/
	CMD_GR_Dismiss_Private kNetInfo;
	kNetInfo.bDismiss = 1;
	CServerItem::get()->SendSocketData(MDM_GR_PRIVATE,SUB_GR_PRIVATE_DISMISS,&kNetInfo,sizeof(kNetInfo));
}
void HNPrivateScenceBase::Button_DismissPrivateNot()
{
	CMD_GR_Dismiss_Private kNetInfo;
	kNetInfo.bDismiss = 0;
	CServerItem::get()->SendSocketData(MDM_GR_PRIVATE,SUB_GR_PRIVATE_DISMISS,&kNetInfo,sizeof(kNetInfo));
}
void HNPrivateScenceBase::Button_JoinNum(char ch)
{
	if (m_iJoinCout >=6)
	{
		return;
	}
	m_kJoinNumTxt[m_iJoinCout] = ch;
	m_kJoinNumTxt[m_iJoinCout+1] = 0;
	m_iJoinCout++;
	upJoinNumTxt();

	if(m_iJoinCout == 6)
	{
		if (m_channelIndex == 1)
		{
			Node* exchangeNode = CSLoader::createNode("exchangeNode.csb");
			Director::getInstance()->getRunningScene()->addChild(exchangeNode, 999);
			exchangeNode->setName("exchangeNode");

			TextAtlas* exchangeNum = (TextAtlas*)exchangeNode->getChildByName("AtlasLabel_ignot");

			Button* btn = (Button*)exchangeNode->getChildByName("Button_ignotadd");
			btn->addClickEventListener([=](Ref* sender){
				int pNum = atoi(exchangeNum->getString().c_str());
				pNum++;
				exchangeNum->setString(StringUtils::format("%d", pNum));
			});

			btn = (Button*)exchangeNode->getChildByName("Button_ignotdel");
			btn->addClickEventListener([=](Ref* sender){
				int pNum = atoi(exchangeNum->getString().c_str());
				pNum--;
				if (pNum <= 10)
					pNum = 10;
				exchangeNum->setString(StringUtils::format("%d", pNum));
			});

			btn = (Button*)exchangeNode->getChildByName("Button_exchange");
			btn->addClickEventListener([=](Ref* sender){
				
				m_exchangeScore = (float) atoi(exchangeNum->getString().c_str());
				if (UserInfo::Instance().getUserScore() < m_exchangeScore)
				{
					NoticeMsg::Instance().ShowTopMsgByScript("word_lobby_ingot_little");
					Director::getInstance()->getRunningScene()->removeChildByName("exchangeNode");
					return;
				}
				m_kActJoinNum = utility::parseInt(m_kJoinNumTxt);
				int iServerID = m_kActJoinNum / 10000 - 10;
				CGameServerItem* pGameServer = GameManagerBase::InstanceBase().SearchGameServer(iServerID);
				if (pGameServer && pGameServer->IsPrivateRoom())
				{
					GameManagerBase::InstanceBase().connectGameServerByServerID(iServerID);
					m_eLinkAction = Type_Link_Join;
				}
				else
				{
					NoticeMsg::Instance().ShowTopMsgByScript("JoinRoomNumError");
				}
				Director::getInstance()->getRunningScene()->removeChildByName("exchangeNode");
			});
		}
		else
		{
			m_kActJoinNum = utility::parseInt(m_kJoinNumTxt);
			int iServerID = m_kActJoinNum / 10000 - 10;
			CGameServerItem* pGameServer = GameManagerBase::InstanceBase().SearchGameServer(iServerID);
			if (pGameServer && pGameServer->IsPrivateRoom())
			{
				GameManagerBase::InstanceBase().connectGameServerByServerID(iServerID);
				m_eLinkAction = Type_Link_Join;
			}
			else
			{
				NoticeMsg::Instance().ShowTopMsgByScript("JoinRoomNumError");
			}
		}
	}
}
void HNPrivateScenceBase::Button_JoinNumDel()
{
	m_iJoinCout--;
	if (m_iJoinCout < 0)
	{
		m_iJoinCout = 0;
	}
	upJoinNumTxt();
}
void HNPrivateScenceBase::Button_JoinNumReset()
{
	m_iJoinCout = 0;
	upJoinNumTxt();
}

void HNPrivateScenceBase::ConnectAndCreatePrivateByKindID(int iKindID,const CMD_GR_Create_Private& kNet)
{
	GameManagerBase::InstanceBase().connectGameServerByKindID(iKindID);
	memcpy(&m_kCreatePrivateNet,&kNet,sizeof(kNet));
	m_eLinkAction = Type_Link_Create;
}
void HNPrivateScenceBase::ConnectAndCreatePrivateByServerID(int iServerID,const CMD_GR_Create_Private& kNet)
{
	GameManagerBase::InstanceBase().connectGameServerByServerID(iServerID);
	memcpy(&m_kCreatePrivateNet,&kNet,sizeof(kNet));
	m_eLinkAction = Type_Link_Create;
}


